package com.jboltai.event.text;

import cn.hutool.core.util.StrUtil;
import com.jboltai.capability.text.TextSplitResult;
import com.jboltai.config.JBoltAIConfig;
import com.jboltai.event.EventType;
import com.jboltai.event.handler.TextSplitEventHandler;

import java.io.File;


/**
 * 文字内容分割事件
 */
public class TextSplitEvent extends TextEvent<TextSplitEvent, TextSplitResult, TextSplitEventHandler> {




    public TextSplitEvent() {
        super(EventType.TEXT_SPLIT);
    }

    private int chunkSize = JBoltAIConfig.getDefaultTextChunkSize();

    private int chunkOverlap = JBoltAIConfig.getDefaultTextChunkOverlap();

    private String[] separators = JBoltAIConfig.getDefaultTextChunkSeparator();

    private String oriText;







    @Override
    public String preCheck() {
        if (file == null && url == null && StrUtil.isBlank(oriText)) {
            return "未指定文件或url或oriText,无法执行";
        }
        return null;
    }

    public int getChunkSize() {
        return chunkSize;
    }

    public TextSplitEvent setChunkSize(int chunkSize) {
        this.chunkSize = chunkSize;
        return this;
    }

    public int getChunkOverlap() {
        return chunkOverlap;
    }

    public TextSplitEvent setChunkOverlap(int chunkOverlap) {
        this.chunkOverlap = chunkOverlap;
        return this;
    }

    public String[] getSeparators() {
        return separators;
    }

    /**
     * 设置分隔符的正则表达式
     * @param separators
     * @return
     */
    public TextSplitEvent setSeparators(String[] separators) {
        if (separators == null) {
            throw new IllegalArgumentException("分割词不可为null");
        }
        this.separators = separators;
        return this;
    }

    public String getOriText() {
        return oriText;
    }

    public TextSplitEvent setOriText(String oriText) {
        if (this.file != null || this.url != null) {
            throw new RuntimeException("已经设置了file或url,无法设置oriText");
        }
        this.oriText = oriText;
        return this;
    }

    @Override
    public TextSplitEvent setFile(File file) {
        if (this.oriText != null || this.url != null) {
            throw new RuntimeException("已经设置了oriText或url,无法设置file");
        }
        this.file = file;
        return this;
    }

    @Override
    public TextSplitEvent setUrl(String url) {
        if (this.oriText != null || this.file != null) {
            throw new RuntimeException("已经设置了oriText或file,无法设置url");
        }
        this.url = url;
        return this;
    }
}
